package com.example.layui.controller;

import com.example.layui.mapper.MenuMapper;
import com.example.layui.mapper.RoleMapper;
import com.example.layui.pojo.Menu;
import com.example.layui.pojo.Role;
import com.example.layui.pojo.UserVo;
import com.example.layui.util.ResponseResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
public class MenuController {

    @Resource
    MenuMapper menuMapper;
    @Resource
    RoleMapper roleMapper;

    @RequestMapping("/demo/table/menu/")
    @ResponseBody
    public ResponseResult menus(Menu menu, Integer page, Integer limit) {
        ResponseResult responseResult = new ResponseResult(0, ""
                , menuMapper.count(menu)
                , menuMapper.queryByPage(menu, (page - 1) * limit, limit));
        return responseResult;
    }


    @RequestMapping("/user/menu/menuform2.html")
    public String menuPower(Model model, Integer id) {
        List<Integer> menuIdList = new ArrayList<>();
        if (id != null) {
            List<Menu> menus = roleMapper.getMenusByRoleId(id);
            for (Menu menu : menus) {
                menuIdList.add(menu.getId());
            }
        }
        List<Role> roles = roleMapper.queryAllRole();
        List<Menu> level1Menus = menuMapper.queryLevel1Menus();
        model.addAttribute("roles", roles);
        model.addAttribute("level1Menus", level1Menus);
        model.addAttribute("menuIdList", menuIdList);
        model.addAttribute("id", id);
        return "views/user/menu/menuform2";
    }

    //做权限分配  --修改role_menu表
    @RequestMapping("/allocation")
    @ResponseBody
    public ResponseResult allocation(int role, int[] level1) {
        List<Menu> menus = roleMapper.getMenusByRoleId(role);
        List<Integer> oldMenuIdList = new ArrayList<>();
        for (Menu menu : menus) {
            oldMenuIdList.add(menu.getId());
        }
        List<Integer> newMenuIdList = new ArrayList<>();
        for (int i = 0; i < level1.length; i++) {
            newMenuIdList.add(level1[i]);
        }
        for (Integer mid : newMenuIdList) {
            if (!oldMenuIdList.contains(mid)) {
                //新增
                roleMapper.addMenu(role, mid);
            }
        }
        for (Integer mid : oldMenuIdList) {
            if (!newMenuIdList.contains(mid)) {
                //删除
                roleMapper.delMenu(role, mid);
            }
        }
        return new ResponseResult(0, "", "");
    }

    @RequestMapping("/user/menu/menuform.html")
    public String addMenu(Model model) {
        //查询所有一级菜单
        List<Menu> Level1List = menuMapper.queryLevel1Menus();
        model.addAttribute("Level1List", Level1List);
        return "views/user/menu/menuform";
    }

    @RequestMapping("/addMenu")
    @ResponseBody
    public ResponseResult addMenu(String menuName, String url, Integer menuType, Integer parentMemu) {
        if (menuType.equals(1)) {
            //添加一级菜单
            menuMapper.addLevel1Menu(menuName, url);
        } else {
            //添加二级菜单
            menuMapper.addLevel2Menu(menuName, url, parentMemu);
        }
        return new ResponseResult(0, "", "");
    }

    @RequestMapping("/user/menu/update")
    public String updateMenu(Integer id, Model model) {
        Menu menu = menuMapper.queryMenuById(id);
        //查询所有一级菜单
        List<Menu> Level1List = menuMapper.queryLevel1Menus();
        model.addAttribute("Level1List", Level1List);
        model.addAttribute("menu", menu);
        return "views/user/menu/menuform3";
    }

    @RequestMapping("/updateMenu")
    @ResponseBody
    public ResponseResult updateMenu(Integer id, String menuName, String url, Integer parentMemu) {
        menuMapper.updateMenu(id, menuName, url, parentMemu);
        return new ResponseResult(0, "", "");
    }

    @RequestMapping("/deleteMenuById")
    @ResponseBody
    public ResponseResult deleteMenuById(Integer id) {
        Menu menu = menuMapper.queryMenuById(id);
        if (menu.getParentId() == 0) { //一级菜单
            //删除其子菜单
            menuMapper.delByParentId(id);
            //删除该菜单在role_menu关联数据
            menuMapper.delRoleMenuByMid(id);
            //删除菜单
            menuMapper.del(id);
        } else {
            //删除菜单
            menuMapper.del(id);
        }
        return new ResponseResult(0, "", "");
    }

    @RequestMapping("/deleteMenus")
    @ResponseBody
    public ResponseResult deleteMenus(@RequestBody List<Menu> menus) {
        for (Menu menu : menus) {
            if (menu.getParentId() == 0) { //一级菜单
                //删除其子菜单
                menuMapper.delByParentId(menu.getId());
                //删除该菜单在role_menu关联数据
                menuMapper.delRoleMenuByMid(menu.getId());
                //删除菜单
                menuMapper.del(menu.getId());
            } else {
                //删除菜单
                menuMapper.del(menu.getId());
            }
        }
        return new ResponseResult(0, "", "");
    }

}
